import xadmin
from .models import *


class GoodsAdmin(object):
    list_display = ['name', "click_num", "sold_num", "fav_num", "storage_num", "market_price",
                    "shop_price", "goods_brief", "goods_desc", "is_new", "is_hot", "add_time"]
    search_fields = ['name']
    list_filter = ['is_new', 'is_hot']
    list_editable = ['is_hot', 'is_new']
    style_fields = {'goods_desc': 'ueditor'}

    class GoodsImagesInline(object):
        model = GoodsImage
        exclude = ["add_time"]
        extra = 1
        style = 'tab'

    inlines = [GoodsImagesInline]


class GoodsCategoryAdmin(object):
    list_display = ["name", "category_type", 'is_tab', "parent_category", "add_time"]
    list_filter = ["category_type", "parent_category", "name"]
    list_editable = ['is_tab', 'parent_category']
    search_fields = ['name', ]


class GoodsBrandAdmin(object):
    list_display = ["category", "image", "name", "desc"]

    def get_context(self):
        context = super(GoodsBrandAdmin, self).get_context()
        if 'form' in context:
            context['form'].fields['category'].queryset = GoodsCategory.objects.filter(category_type=1)
        return context


class BannerGoodsAdmin(object):
    list_display = ["goods", "image", "index"]


class HotSearchWordsAdmin(object):
    list_display = ["keywords", "index", "add_time"]


class IndexADAdmin(object):
    list_display = ["category", "goods"]


xadmin.site.register(Goods, GoodsAdmin)
xadmin.site.register(GoodsCategory, GoodsCategoryAdmin)
xadmin.site.register(Banner, BannerGoodsAdmin)
xadmin.site.register(GoodsCategoryBrand, GoodsBrandAdmin)
xadmin.site.register(IndexAD, IndexADAdmin)
xadmin.site.register(HotSearchWords, HotSearchWordsAdmin)
